package com.xo.xodemo.controller.process;

import com.xo.xodemo.common.Result;
import com.xo.xodemo.mapper.db1.HasBeenReadMapper;
import com.xo.xodemo.model.HasBeenReadModel;
import com.xo.xodemo.springsecurityconfig.CustomUserDetails;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/HasBeenRead")
public class HasBeenReadController {
    @Autowired
    private HasBeenReadMapper hasBeenReadMapper;

    //查询未读消息
    @RequestMapping("/selectHasBeenRead")
    public List<Map> selectHasBeenRead(@AuthenticationPrincipal CustomUserDetails userDetails){
        List resultList = new ArrayList<>();
        //获取当前用户所有的未读信息
        List<HasBeenReadModel> hasBeenReadList =  hasBeenReadMapper.selectHasBeenRead(userDetails.getUsername());
        if (hasBeenReadList.size() != 0){
            List<HasBeenReadModel> unfinishedList = new ArrayList<>();
            Map<String,List<HasBeenReadModel>> unfinishedMap = new HashMap<>();
            Map<String,Integer> unfinishedNumMap = new HashMap<>();
            List<HasBeenReadModel> finishedList = new ArrayList<>();
            Map<String,List<HasBeenReadModel>> finishedMap = new HashMap<>();
            Map<String,Integer> finishedNumMap = new HashMap<>();
            List<HasBeenReadModel> copyList = new ArrayList<>();
            Map<String,List<HasBeenReadModel>> copyMap = new HashMap<>();
            Map<String,Integer> copyNumMap = new HashMap<>();
            //通过未读信息类型分别获取到（未完成messageType=1，已完成messageType=2，抄送给我messageType=3）的未读信息
            for (HasBeenReadModel hasBeenReadModel:hasBeenReadList){
                switch (hasBeenReadModel.getMessageType()){
                    case "1":
                        unfinishedList.add(hasBeenReadModel);
                        break;
                    case "2":
                        finishedList.add(hasBeenReadModel);
                        break;
                    case "3":
                        copyList.add(hasBeenReadModel);
                        break;
                }
            }
            unfinishedMap.put("unfinishedData",unfinishedList);
            unfinishedNumMap.put("unfinishedNum",unfinishedList.size());
            finishedMap.put("finishedData",finishedList);
            finishedNumMap.put("finishedNum",finishedList.size());
            copyMap.put("copyData",copyList);
            copyNumMap.put("copyNum",copyList.size());
//            resultList.add(unfinishedMap);
            resultList.add(unfinishedNumMap);
//            resultList.add(finishedMap);
            resultList.add(finishedNumMap);
//            resultList.add(copyMap);
            resultList.add(copyNumMap);
        }
        return resultList;
    }

    //根据用户名，消息id删除未读记录
    @RequestMapping("/deleteHasBeenRead")
    public Result deleteHasBeenRead(@AuthenticationPrincipal CustomUserDetails userDetails,@Param("messageId") String messageId){
        hasBeenReadMapper.deleteHasBeenRead(userDetails.getUsername(),messageId);
        return Result.success();
    }
}
